package com.parenting.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.parenting.entities.CommodityDO;
import com.parenting.model.IMPage;
import com.parenting.oauth2.config.AuthUtil;
import com.parenting.request.AddCommodityRequest;
import com.parenting.request.SearchCommodityRequest;
import com.parenting.request.UpLowerCommodity;
import com.parenting.request.UpdateCommodityRequest;
import com.parenting.response.CommodityListResponse;
import com.parenting.response.CommodityResponse;
import com.parenting.service.CommodityService;
import com.parenting.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-08-27
 */
@RestController
@Api(tags = "活动管理-商品管理", description = "商品的增删改查")
@RequestMapping("/commodity")
public class CommodityController {


    @Autowired
    private CommodityService commodityService;

    /***
     * 商品添加
     * @param request  添加对象
     */
    @ApiOperation("商品添加")
    @PostMapping
   public Result addCommodity(@RequestBody  AddCommodityRequest request){
       commodityService.addCommodity(request, AuthUtil.getUserInfo().getShopId());
       return  Result.success();
   }

    /***
     * 商品修改
     * @param request 修改对象
     */
    @ApiOperation("商品修改")
    @PutMapping
    public Result  updateCommodity(@RequestBody  UpdateCommodityRequest request){
        commodityService.updateCommodity(request);
        return  Result.success();
    }

    /***
     * 商品删除  支持批量删除
     * @param commodityId 商品ID集合
     */
    @ApiOperation("商品删除")
    @DeleteMapping
    public Result  deleteCommodity(@RequestBody  List<String> commodityId){
        commodityService.deleteCommodity(commodityId);
        return  Result.success();
    }

    /***
     * 商品列表
     * @param searchCommodityRequest
     * @return
     */
    @ApiOperation("商品列表")
    @PostMapping("list")
    public Result<IMPage<CommodityListResponse>> getCommodityListByCondition(@RequestBody  SearchCommodityRequest searchCommodityRequest){
        IMPage<CommodityListResponse> commodityListByCondition = commodityService.getCommodityListByCondition(searchCommodityRequest);
        return  Result.buildSuccess(commodityListByCondition);
    }

    /***
     * 查看商品详情
     * @param commodityId 商品ID
     * @return
     */
    @ApiOperation("商品详情")
    @GetMapping("{commodityId}")
    public  Result<CommodityResponse> getCommodityByCommodityId(@PathVariable("commodityId") String commodityId){

        return  Result.buildSuccess(commodityService.getCommodityByCommodityId(commodityId));
    }



    @ApiOperation("上下架修改")
    @PostMapping("up")
    public  Result UpLowerCommodityByCommodityId(@RequestBody UpLowerCommodity up){
        LambdaUpdateWrapper<CommodityDO>  wrapper =new LambdaUpdateWrapper();
        wrapper.eq(CommodityDO::getId,up.getId());
        wrapper.set(CommodityDO::getShelves,up.getShelves());
        commodityService.update(wrapper);
        return Result.buildSuccess();

    }



}

